package com.meepo.feed.comment

import android.content.Context
import android.os.Bundle
import android.view.View
import com.meepo.basic.router.RouterManager
import com.meepo.basic.router.RouterManager.FeedModule.FeedCommentReply_Path
import com.meepo.basic.components.listfragment.ListFragmentParams
import com.meepo.basic.components.view.SwipeBackUnSupportActivity
import com.meepo.feed.API
import com.meepo.framework.design.input.InputDialog
import com.meepo.framework.design.userinfo.UserInfo
import com.meepo.feed.R
import com.meepo.basic.schema.feed.Comment
import com.meepo.basic.schema.feed.CommentReq
import com.meepo.sdk.utils.ActivityHelper
import com.xiaojinzi.component.anno.RouterAnno
import com.xiaojinzi.component.support.ParameterSupport
import kotlinx.android.synthetic.main.comment_reply_activity.*


@RouterAnno(hostAndPath = FeedCommentReply_Path)
class CommentReplyActivity : SwipeBackUnSupportActivity() {



    private var mCommentReplyListFragment: CommentReplyListFragment? = null

    private lateinit var mComment: Comment
    private lateinit var mCommentId: String

    companion object {
        fun launch(context: Context, commentId: String) {
            ActivityHelper.launch(context, CommentReplyActivity::class.java, "commentId", commentId)
        }
    }


    private fun applyMainComment(comment: Comment?) {

        if (comment == null) {
            // todo
            finishAndRemoveTask()
        }

        comment?.let {
            mComment = comment

            // 用户信息
            comment.author?.let {
                userinfo_view.setUserInfo(
                    UserInfo(
                        username = it.nickname ?: "",
                        avatar = it.avatar ?: "",
                        desc = comment.createdAt ?: "",
                        tag = it.getTag() ?: ""
                    )
                )

            }

            val hintText = "回复 ${mComment.author?.nickname ?: ""} :"
            comment_input_layout.setOnClickListener {
                showInputDialog(hintText)
            }
            comment_input_layout.hint = hintText

            // 评论内容
            content_tv.text = comment.content

        }
    }


    override fun initView(view: View, savedInstanceState: Bundle?) {
        back_iv.setOnClickListener { finish() }
        mCommentId = ParameterSupport.getString(intent, "commentId", null)!!


        mCommentReplyListFragment = RouterManager.FeedModule.routeToCommentReplyList(
            mainCommentId = mCommentId,
            listFragmentParams = ListFragmentParams(enablePullRefresh = false)
        ) as CommentReplyListFragment
        loadRootFragment(R.id.comment_list_frame, mCommentReplyListFragment!!)
    }

    private fun showInputDialog(textHint: String = "") {
        InputDialog(textHint, object : InputDialog.InputListener {
            override fun onSubmit(inputText: String) {

                mCommentId = ParameterSupport.getString(intent, "commentId", null)!!

                presenter.call(
                    API.instance().addComment(
                        CommentReq(
                            itemId = "",
                            content = inputText,
                            mainCommentId = mCommentId,
                            atCommentId = ""
                        )
                    ),
                    { rsp -> rsp.data?.let { mCommentReplyListFragment?.insert(it) } },
                    { }

                )
            }

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            }

        }).show(supportFragmentManager, "aaa")
    }

    override fun initData() {

        presenter.call(
            API.instance().fetchCommentDetail(mCommentId),
            { rsp -> this.applyMainComment(rsp.data) },
            { finish() }

        )

    }

    override fun setUpContentLayout(): Int = R.layout.comment_reply_activity

}